#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from models.dbconfig import dbConfig
'''
class\r\n
    db(dbConfig)\r\n 
description\r\n
    mongodb db helper class\r\n
'''
class db(dbConfig):
    
    def __init__(self):
        super(db,self).__init__()

    def setCollection(self,collectionName):
        if self.db:
            self.collection=self.db[collectionName]

    def save(self,args,flag="one"):
        '''
        Feature\r\n
            save(self,args,flag)\r\n
        Description\r\n
            保存\r\n
        Args\r\n
            args\r\n
                type:Model Class\r\n
                description:实体\r\n
            flag\r\n
                type:string,[one|many]\r\n
                description:one->insert one record,many->insert some records\r\n
                example:save([model1,model2...,modeln])\r\n
        '''
        if self.collection:
            if flag.lower()=="one":
                return self.collection.insert_one(args)
            elif flag.lower()=="many":
                return self.collection.insert_many(args)
        return None
   
    def update(self,query,args):
        if self.collection:
           return self.collection.update(query,{'$set':args})
        return None

    def remove(self,query=None):
        if self.collection:
            if query:
               return self.collection.remove(query)
            else:
                return self.collection.remove()
        return None

    def findOne(self,query):
        if self.collection:
            return self.collection.find_one(query)
        return None
    
    def find(self,**kwargs):
        try:
            if self.collection:
                if "query" in kwargs:
                    if "limit" in kwargs:
                        return self.collection.find(kwargs["query"]).limit(kwargs["limit"])
                    return self.collection.find(kwargs["query"])
                return None
        except Exception as e:
            print(e)
            return None
    '''
    def find(self,query=None,sort=None,skip=None,limit=None):  
        if self.collection:
            if limit:
                if query:
                    if sort:
                        return self.collection.find(query).sort(sort).skip(skip).limit(limit)
                    else:
                        return self.collection.find(query).skip(skip).limit(limit)
                elif sort:
                    if sort:
                        return self.collection.find(query).sort(sort).skip(skip).limit(limit)
                    else:
                        return self.collection.find(query).skip(skip).limit(limit)
            elif skip:
                if query:
                    if sort:
                        return self.collection.find(query).sort(sort).skip(skip)
                    else:
                        return self.collection.find(query).skip(skip)
                elif sort:
                    if sort:
                        return self.collection.find(query).sort(sort).skip(skip)
                    else:
                        return self.collection.find(query).skip(skip)
            elif sort:
                if query:
                    if sort:
                        return self.collection.find(query).sort(sort)
                    else:
                        return self.collection.find(query)
                elif sort:
                    if sort:
                        return self.collection.find(query).sort(sort)
                    else:
                        return self.collection.find(query)
            elif query:
                return self.collection.find(query)
            else:
                return self.collection.find(query)
            '''